package net.qhzw.vip.qiyu.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.config.WxProperties;
import net.qhzw.vip.qiyu.mapper.OrderMapper;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.OrderRefundsService;
import net.qhzw.vip.qiyu.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@ApiIgnore
@RestController
@RequestMapping("/notice/pay")
@Slf4j
public class PayNoticeController {

//    @Autowired
//    private WxPayService wxMiniPayService;

    @Autowired
    private PayService payService;

    @Autowired
    private OrderRefundsService orderRefundsService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private WxProperties properties;


    //@RequestMapping("/order")
    public JsonResult submit(@RequestBody String xmlData) throws WxPayException {
        System.out.println("支付回调: " + xmlData);
        log.info("【支付回调】： ", xmlData);
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setKeyPath(properties.getKeyPath());
        payConfig.setSignType(WxPayConstants.SignType.MD5);
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("MD5");
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        WxPayOrderNotifyResult parseOrderNotifyResult = wxPayService.parseOrderNotifyResult(xmlData);
//        WxPayOrderNotifyResult parseOrderNotifyResult = null;
        String payNo = parseOrderNotifyResult.getOutTradeNo();
        String bizPayNo = parseOrderNotifyResult.getTransactionId();
        // 根据内部订单号更新order settlement
        payService.paySuccess(payNo, bizPayNo);
        return JsonResult.ok();
    }

    /**
     * 处理支付回调
     *
     * @param xmlData
     * @return
     */
    @ApiOperation(value = "处理支付回调")
    @RequestMapping("/order")
    public JsonResult notifyOrder(@RequestBody String xmlData) {
        log.info("【支付回调】： {}", xmlData);
        WxPayOrderNotifyResult rs = WxPayOrderNotifyResult.fromXML(xmlData);
        String payNo = rs.getOutTradeNo();
        String bizPayNo = rs.getTransactionId();
        List<String> strings = payService.paySuccess(payNo, bizPayNo);
        return JsonResult.ok(strings);
    }

    /**
     * 处理支付回调
     *
     * @param xmlData
     * @return
     */
    @ApiOperation(value = "处理支付回调")
    @RequestMapping("/order1")
    public JsonResult notifyOrder1(String payNo, String bizPayNo) {
        List<String> strings = payService.paySuccess(payNo, bizPayNo);
        return JsonResult.ok(strings);
    }


    @ApiOperation(value = "处理退款回调")
    @PostMapping("/refund")
    public JsonResult refund(@RequestBody String xmlData) {
        log.info("【处理退款回调】： {}", xmlData);
        /*WxPayRefundNotifyResult wxPayRefundNotifyResult = null;
        try {
            WxPayConfig payConfig = new WxPayConfig();
            payConfig.setAppId(properties.getAppId());
            payConfig.setMchId(properties.getMchId());
            payConfig.setMchKey(properties.getMchKey());
            payConfig.setKeyPath(properties.getKeyPath());
            payConfig.setSignType(WxPayConstants.SignType.MD5);
            payConfig.setTradeType("JSAPI");
            payConfig.setSignType("MD5");
            WxPayService wxPayService = new WxPayServiceImpl();
            wxPayService.setConfig(payConfig);
            wxPayRefundNotifyResult = wxPayService.parseRefundNotifyResult(xmlData);
            if (wxPayRefundNotifyResult.getReturnCode().equals("SUCCESS")) {
                WxPayRefundNotifyResult.ReqInfo reqInfo = wxPayRefundNotifyResult.getReqInfo();
                String outRefundNo = reqInfo.getOutRefundNo(); //1560191452535984128
                OrderRefunds orderRefunds = orderRefundsService
                        .getOne(new LambdaQueryWrapper<OrderRefunds>().eq(OrderRefunds::getRefundTradeNo, outRefundNo));
                String orderNumber = orderRefunds.getOrderNumber();
                //orderMapper.refund(orderNumber);
            }
        } catch (Exception e) {
            log.error("退款失败，失败信息:{}", e);
        }*/
        return JsonResult.ok();
    }

}
